package com.yimi.yinhepda.adapter.check_balance;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import com.bumptech.glide.Glide;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 000432
 * @date 2017/11/4
 * Description
 */

public class GlideSimpleAdapter extends SimpleAdapter {

  List<? extends Map<String, ?>> data;
  LayoutInflater minflater;
  int resource;
  List<String> pickPics;

  public GlideSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource,
      String[] from, int[] to, List<String> pickPics) {
    super(context, data, resource, from, to);
    this.data = data;
    this.minflater = LayoutInflater.from(context);
    this.resource = resource;
    this.pickPics = pickPics;

    setGlideViewBinder(context);
  }

  private void setGlideViewBinder(final Context context) {
    ViewBinder viewBinder = new ViewBinder() {
      @Override public boolean setViewValue(View view, Object o, String s) {
        if (view instanceof ImageView && o instanceof Bitmap) {
          ImageView img = (ImageView) view;
          img.setImageBitmap((Bitmap) o);
          //返回true表示绑定已经完成，将不再调用系统默认的绑定实现
          return true;
        } else if (view instanceof ImageView && o instanceof String) {
          ImageView img = (ImageView) view;
          //此处是拍照或者选择照片后返回的url
          String imgUrl = (String) o;
          Glide.with(context).load(imgUrl).into(img);
          return true;
        }
        return true;
      }
    };
    setViewBinder(viewBinder);
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {

    if(data.size() != pickPics.size() && position ==(data.size()-1)) {
      return minflater.inflate(resource,null);
    } else {
      return super.getView(position, convertView, parent);
    }
  }

}
